<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2017/10/26
  Time: 10:26
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/bootstrap/css/bootstrap.css">
    <style type="text/css">
        #tb {
            width: 60%;
            text-align: center;
            margin: 20px auto;

        }

        a {
            text-decoration: none;
        }
    </style>
</head>
<body>

<h1 style="text-align: center">商品列表 </h1>
<div id="tb">
    <table class="table table-hover">

        <thead>
        <tr>
            <td width="80px">序号</td>
            <td width="320px">图书名称</td>
            <td width="160px">商场价格</td>
            <td width="160px">操作</td>
        </tr>
        </thead>

        <c:forEach var="p" items="${page.pageList}" varStatus="status">
            <%-- <c:choose>
                 <c:when test="${status.count%2 == 0}">
                     <tr style="background-color: aliceblue">
                 </c:when>
                 <c:otherwise>
                     <tr style="background-color: darkgray">
                 </c:otherwise>
             </c:choose>--%>
            <tr>
                <td>${status.count}</td>
                <td>${p.pname}</td>
                <td>${p.shopprice}</td>
                <td>
                    <a href="${pageContext.request.contextPath}/editPageUIServlet?pid=${p.pid}&currentPage=${page.currenPage}">修改</a>|
                    <a href="" onclick="return confirm('确认删除码？？')">删除</a>
                </td>
            </tr>
        </c:forEach>

    </table>

</div>

<hr>
第${page.currenPage}页/共${page.pageCount}页
<hr>
<div>
    <a href="${pageContext.request.contextPath}/pageServlet?currentPage=1">首页</a>
    <%--当前页是第1页的时候，没有【上一页】--%>
    <c:if test="${page.currenPage>1}">
        <a href="${pageContext.request.contextPath}/pageServlet?currentPage=${page.currenPage-1}">上一页</a>
    </c:if>


    <%--当总页码小于等于10页，全部显示   1 ----- pageTotal
       总页数大于10怎么显示？？
         显示的范围为： 开始的位置 begin： 当前页-5
                        结束的位置end ：  当前页+4
          分析：当begin <0  begin = 1   end = 10
                当end > 总页数的时候： end = 总页数   begin  = 总页数 - 9

    --%>
    <c:choose>
        <%--当总页数小于等于10的时候，显示全部--%>
        <c:when test="${page.pageCount<=10}">
            <c:set var="begin" value="1"/>
            <c:set var="end" value="${page.pageCount}"/>
        </c:when>
        <%--当总页数大于10的时候，只显示10页--%>
        <c:otherwise>
            <%--显示策略--%>
            <c:set var="begin" value="${page.currenPage -5}"/>
            <c:set var="end" value="${page.currenPage +4}"/>

            <c:if test="${begin<1}">
                <c:set var="begin" value="1"/>
                <c:set var="end" value="10"/>
            </c:if>
            <c:if test="${end > page.pageCount}">
                <c:set var="begin" value="${page.pageCount-9}"/>
                <c:set var="end" value="${page.pageCount}"/>
            </c:if>
        </c:otherwise>

    </c:choose>
    <%--    int begin ;
        int end;
        if(count <=10){
          begin = 1;
          end = total;
        }else{
           begin = current - 5;
           end = current +4;
           if(begin <1){
                begin = 1;
                end = 10;
            }
            if(end > total){
                begin = total - 9;
                end = total;
            }
        }--%>

    <%--显示页码--%>
    <c:forEach var="num" begin="${begin}" end="${end}">

        <%--如果在显示页码的时候，是当前页，则不显示超连接--%>

        <c:choose>
            <c:when test="${num == page.currenPage}">
                ${num}
            </c:when>
            <c:otherwise>
                <a href="${pageContext.request.contextPath}/pageServlet?currentPage=${num}">[ ${num}]</a>
            </c:otherwise>
        </c:choose>

    </c:forEach>
    <%--当前页是最后一页的时候，没有【下一页】--%>
    <c:if test="${page.currenPage<page.pageCount}">
        <a href="${pageContext.request.contextPath}/pageServlet?currentPage=${page.currenPage+1}">下一页</a>
    </c:if>

    <a href="${pageContext.request.contextPath}/pageServlet?currentPage=${page.pageCount}">尾页</a>

    <hr>


    <%--使用bootstrap布局--%>
    <nav aria-label="...">
       <%-- <a href="${pageContext.request.contextPath}/pageServlet?currentPage=1">首页</a>--%>
        <ul class="pagination">
            <%--首页--%>
            <c:if test="${page.currenPage==1}">
                <li class="disabled">
                    <span aria-label="Previous"><span aria-hidden="true">&laquo;</span></span>
                </li>
            </c:if>
            <c:if test="${page.currenPage>1}">
                <li class="">
                    <a href="${pageContext.request.contextPath}/pageServlet?currentPage=1">首页
                        <span class="sr-only">(current)</span></a>
                </li>
            </c:if>

            <%--上一页--%>
            <c:if test="${page.currenPage>1}">
                <li class=""><a href="${pageContext.request.contextPath}/pageServlet?currentPage=${page.currenPage-1}" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>
            </c:if>
            <%--显示页码--%>
            <c:forEach var="num" begin="${begin}" end="${end}">

                <%--如果在显示页码的时候，是当前页，则不显示超连接--%>

                <c:choose>
                    <c:when test="${num == page.currenPage}">
                        <li class="active"><span>${num} <span class="sr-only">${num}</span></span></li>
                    </c:when>
                    <c:otherwise>
                        <li class=""><a href="${pageContext.request.contextPath}/pageServlet?currentPage=${num}">${num}
                            <span class="sr-only">(current)</span></a></li>
                    </c:otherwise>
                </c:choose>

            </c:forEach>
            <%--下一页--%>
            <c:if test="${page.currenPage<page.pageCount}">
                <li class=""><a href="${pageContext.request.contextPath}/pageServlet?currentPage=${page.currenPage+1}"
                                aria-label="Previous"><span aria-hidden="true">&raquo;</span></a></li>
            </c:if>

            <%--尾页--%>
                <%--首页--%>
                <c:if test="${page.currenPage==page.pageCount}">
                    <li class="disabled">
                        <span aria-label="Previous"><span aria-hidden="true">&laquo;</span></span>
                    </li>
                </c:if>
                <c:if test="${page.currenPage<page.pageCount}">
                    <li class="">
                        <a href="${pageContext.request.contextPath}/pageServlet?currentPage=${page.pageCount}">尾页
                            <span class="sr-only">(current)</span></a>
                    </li>
                </c:if>
        </ul>
       <%-- <a href="${pageContext.request.contextPath}/pageServlet?currentPage=${page.pageCount}">尾页</a>--%>
    </nav>


</div>
</body>
<script type="text/javascript" src="${pageContext.request.contextPath}/bootstrap/js/jquery-3.2.1.min.js"/>
<script type="text/javascript" src="${pageContext.request.contextPath}/bootstrap/js/bootstrap.js"/>
</html>
